package com.example;;

import net.fabricmc.api.ModInitializer;
import net.minecraft.block.Blocks;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VecMod implements ModInitializer {
	public static final String MOD_ID = "vecmod";
	public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

	private static final java.util.Map<java.util.UUID, Long> cooldowns = new java.util.HashMap<>();

	@Override
	public void onInitialize() {
		net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents.START_SERVER_TICK.register(server -> {
			long currentTime = server.getOverworld().getTime();

			for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
				BlockPos pos = player.getBlockPos().down();

				// 使用 getWorld() 而不是直接访问 world 字段
				net.minecraft.world.World world = player.getWorld();

				long lastTriggerTime = cooldowns.getOrDefault(player.getUuid(), 0L);
				if (currentTime - lastTriggerTime < 600) {
					continue;
				}

				if (world.getBlockState(pos).getBlock() == Blocks.END_ROD) {
					if (player.fallDistance < 0 || player.isSneaking()) {
						player.addStatusEffect(new StatusEffectInstance(
								StatusEffects.REGENERATION, 600, 2, false, true, true
						));
						player.fallDistance = 0;
						cooldowns.put(player.getUuid(), currentTime);

						world.playSound(null, pos,
								net.minecraft.sound.SoundEvents.ENTITY_ENDER_DRAGON_FLAP,
								net.minecraft.sound.SoundCategory.BLOCKS, 1.0F, 1.0F);
					}
				}
			}
		});

		LOGGER.info("VecMod initialized!");
	}
}